package br.com.news.web.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import br.com.news.utils.database.beans.NoticiaBean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public class NoticiaService {
	
	public List<NoticiaBean> lastNews(int qntNoticias,long idNoticia,long idCategoria) {
	    Client c = Client.create();
	    String json = c.resource("http://localhost:8080/news-web-service-rest/noticias/noticiasPorPagina/"+qntNoticias+"/"+idNoticia+"/"+idCategoria).accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
	    Gson gson = new Gson();
	    List<NoticiaBean> noticias = gson.fromJson(json, new TypeToken<ArrayList<NoticiaBean>>(){}.getType());
	    
	    /*for (NoticiaBean noticia : noticias) {
	    	String[] qnt = noticia.getTitulo().trim().split("               ");
	    	if (qnt.length >1) {
		    	noticia.setSubTitulo(noticia.getTitulo().trim().split("               ")[1]);
		    	noticia.setTitulo(noticia.getTitulo().trim().split("               ")[0]);
	    	}
	    }*/
	    
	    return noticias;
	}
	
	public NoticiaBean findById(Long id) {
	    Client c = Client.create();
	    String json = c.resource("http://localhost:8080/news-web-service-rest/noticias/noticiaById/"+id.toString()).accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
	    Gson gson = new Gson();
	    NoticiaBean noticias = gson.fromJson(json, new TypeToken<NoticiaBean>(){}.getType());
	    
	    
	    return noticias;
	}
	
	public Integer quantidadePaginas(long idCategoria) {
	    Client c = Client.create();
	    String json = c.resource("http://localhost:8080/news-web-service-rest/noticias/quantidadePaginas/"+idCategoria).accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
	    Gson gson = new Gson();
	    return gson.fromJson(json, new TypeToken<Integer>(){}.getType());
	}
	
	public List<NoticiaBean> noticiasByPagina(int linha,long idCategoria, int quantidade) {
	    Client c = Client.create();
	    String json = c.resource("http://localhost:8080/news-web-service-rest/noticias/noticiasByPagina/"+linha+"/"+idCategoria+"/"+quantidade).accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
	    Gson gson = new Gson();
	    List<NoticiaBean> noticias = gson.fromJson(json, new TypeToken<ArrayList<NoticiaBean>>(){}.getType());
	    
	    for (NoticiaBean noticia : noticias) {
	    	String[] qnt = noticia.getTitulo().trim().split("               ");
	    	if (qnt.length >1) {
		    	noticia.setSubTitulo(noticia.getTitulo().trim().split("               ")[1]);
		    	noticia.setTitulo(noticia.getTitulo().trim().split("               ")[0]);
	    	}
	    }
	    
	    return noticias;
	}
	
	public String salvar(NoticiaBean noticiaBean) {
		Gson g = new Gson();
		String json = g.toJson(noticiaBean);
		Client c = Client.create();
		String respostaJson = c.resource("http://localhost:8080/news-web-service-rest/noticias/insert").accept(MediaType.APPLICATION_JSON+ ";charset=utf-8")
				.type(MediaType.APPLICATION_JSON).post(String.class, json);
		String retorno = g.fromJson(respostaJson, String.class);
		return retorno;
	}
	
	public String update(NoticiaBean noticiaBean) {
		Gson g = new Gson();
		String json = g.toJson(noticiaBean);
		Client c = Client.create();
		String respostaJson = c.resource("http://localhost:8080/news-web-service-rest/noticias/update").accept(MediaType.APPLICATION_JSON+ ";charset=utf-8")
				.type(MediaType.APPLICATION_JSON).post(String.class, json);
		String retorno = g.fromJson(respostaJson, String.class);
		return retorno;
	}
	
	public List<NoticiaBean> findByExample(NoticiaBean noticiaBean){
		Gson g = new Gson();
		String json = g.toJson(noticiaBean);

		Client c = Client.create();
		String respostaJson = c.resource("http://localhost:8080/news-web-service-rest/noticias/findByExample").accept(MediaType.APPLICATION_JSON+ ";charset=utf-8")
				.type(MediaType.APPLICATION_JSON).post(String.class, json);
		List<NoticiaBean> noticiaBeans = g.fromJson(respostaJson, new TypeToken<ArrayList<NoticiaBean>>() {}.getType());

		return noticiaBeans;
	}
	
	public List<NoticiaBean> buscaNoticiaPorNome(String nome) {
	    Client c = Client.create();
	    String json = c.resource("http://localhost:8080/news-web-service-rest/noticias/noticiaPorNome/"+nome).accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
	    Gson gson = new Gson();
	    List<NoticiaBean> noticias = gson.fromJson(json, new TypeToken<ArrayList<NoticiaBean>>(){}.getType());
	    
	    for (NoticiaBean noticia : noticias) {
	    	String[] qnt = noticia.getTitulo().trim().split("               ");
	    	if (qnt.length >1) {
		    	noticia.setSubTitulo(noticia.getTitulo().trim().split("               ")[1]);
		    	noticia.setTitulo(noticia.getTitulo().trim().split("               ")[0]);
	    	}
	    }
	    
	    return noticias;
	}
	
	public String returnTextNew(Long idNoticia) {
		Client c = Client.create();
		String retorno = c.resource("http://localhost:8080/news-web-service-rest/noticias/returnTextNew/"+idNoticia+"/").accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
		if (retorno.equals("NOK")) {
			retorno = "Não foi possível carregar a noticia, favor entrar em contato com o administrador do sistema!";
		}
		return retorno;
	}
}
